<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改资产')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-assets-edit" th:object="${assetsInfo}">
            <h4 class="form-header h4">资产信息</h4>
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">资产名称：</label>
                <div class="col-sm-8">
                    <input name="assetsName" th:field="*{assetsName}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">资产类别：</label>
                <div class="col-sm-8">
                    <select name="assetsType" class="form-control m-b" th:with="type=${@dict.getType('assets_type')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{assetsType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">规格型号：</label>
                <div class="col-sm-8">
                    <input name="assetsModel" th:field="*{assetsModel}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">SN号：</label>
                <div class="col-sm-8">
                    <input name="assetsSn" th:field="*{assetsSn}" class="form-control" type="text" readonly required onclick="selectSn()">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">计量单位：</label>
                <div class="col-sm-8">
                    <input name="assetsUnit" th:field="*{assetsUnit}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">供应商：</label>
                <div class="col-sm-8">
                    <select name="assetsSource" required class="form-control m-b" th:with="type=${@supplier.getSupplier()}">
                        <option value="">请选择</option>
                        <option th:each="supplier : ${type}" th:text="${supplier.supplierName}" th:value="${supplier.supplierId}" th:field="*{assetsSource}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">购入日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="assetsBuytime" th:value="${#dates.format(assetsInfo.assetsBuytime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">有效期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="assetsExptime" th:value="${#dates.format(assetsInfo.assetsExptime, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">金额：</label>
                <div class="col-sm-8">
                    <input name="assetsPrice" th:field="*{assetsPrice}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">所属区域：</label>
                <div class="col-sm-8">
                    <select name="assetsArea" class="form-control m-b" th:with="type=${@dict.getType('area_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{assetsArea}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">所属公司：</label>
                <div class="col-sm-8">
                    <select name="assetsCompany" class="form-control m-b" th:with="type=${@dict.getType('company')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{assetsCompany}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">管理员：</label>
                <div class="col-sm-8">
                    <input name="assetsAdmin" th:field="*{assetsAdmin}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">存放点：</label>
                <div class="col-sm-8">
                    <input name="assetsStorage" th:field="*{assetsStorage}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">资产照片：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="assetsImg" th:field="*{assetsImg}">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="assetsImg" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <textarea name="assetsDesc" class="form-control">[[*{assetsDesc}]]</textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">设备状态：</label>
                <div class="col-sm-8">
                    <select name="assetsStatus" disabled="disabled" class="form-control m-b" th:with="type=${@dict.getType('assets_status')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{assetsStatus}"></option>
                    </select>
                </div>
            </div>
            <h4 class="form-header h4">资产使用信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <!--<button type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>-->
                    <!--<button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>-->
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "assets/assets";
        var receiveTypeDatas = [[${@dict.getType('receive_type')}]];
        $("#form-assets-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-assets-edit').serialize());
            }
        }

        function selectSn(){
            var scannerEnabled = [[${scannerEnabled}]];
            if(!scannerEnabled){
                return;
            }
            $.modal.loading(" 等待扫描标签编号......");
            //生成随机数
            var snKey = new Date().Format("yyyyMMddHHmmss");
            //发接口生成获取扫描key
            $.ajax({
                type: "get",
                url: prefix + "/makeSnKey/"+snKey,
                data: {},
                success: function(data) {
                    //轮询扫描结果
                    var timer = setInterval(function(){getSn(snKey,timer)},2000);
                }
            });
        }

        function getSn(snKey,timer){
            $.ajax({
                type: "get",
                url: prefix + "/getSnKey/"+snKey,
                data: {},
                success: function(data) {
                    //轮询扫描结果
//                    alert(JSON.stringify(data));
                    if(data.data != undefined){
                        $("*[name='assetsSn']").val(data.data);
                        clearInterval(timer);
                        $.modal.closeLoading();
                    }
                }
            });
        }

        $("input[name='assetsBuytime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='assetsExptime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': ctx + 'common/upload',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            $(this).fileinput('_initFileActions');
        });

        $(function() {
            //alert(JSON.stringify([[${assetsInfo.assetsReceiveList}]]));
            var options = {
                data: [[${assetsInfo.assetsReceiveList}]],
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                sortName: "createTime",                         // 排序列名称
                sortOrder: "desc",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'userName',
                    align: 'center',
                    title: '领用人',
                    formatter: function(value, row, index) {
                        //var html = $.common.sprintf("<input class='form-control' readonly='readonly' type='text' name='assetsReceiveList[%s].userName' value='%s'>", index, value);
                        return value;
                    }
                },
                    {
                        field: 'receiveNo',
                        align: 'center',
                        title: '领用编号',
                        formatter: function(value, row, index) {
//                            var html = $.common.sprintf("<input class='form-control' readonly='readonly' type='text' name='assetsReceiveList[%s].receiveNo' value='%s'>", index, value);
                            return value;
                        }
                    },
                {
                    field: 'receiveTime',
                    align: 'center',
                    title: '领用日期',
                    formatter: function(value, row, index) {
//                        var html = $.common.sprintf("<input class='form-control' readonly='readonly' type='text' name='assetsReceiveList[%s].receiveTime' value='%s'>", index, value);
                        return value;
                    }
                },
                {
                    field: 'createTime',
                    align: 'center',
                    title: '创建时间',
                    formatter: function(value, row, index) {
//                        var html = $.common.sprintf("<input class='form-control' readonly='readonly' type='text' name='assetsReceiveList[%s].createTime' value='%s'>", index, value);
                        return value;
                    }
                },
                {
                    field: 'receiveType',
                    align: 'center',
                    title: '类型',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(receiveTypeDatas, value);
                    }
                }

//                {
//                    title: '操作',
//                    align: 'center',
//                    formatter: function(value, row, index) {
//                        var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
//                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
//                    }
//                }
                ]
            };
            $.table.init(options);
        });

        function addRow() {
            var count = $("#" + table.options.id).bootstrapTable('getData').length;
            var row = {
                index: $.table.serialNumber(count),
                userId: "",
                receiveTime: "",
                receiveNo: "",
                receiveType: "",
                createBy: "",
                createTime: "",
                receiveDesc: "",
                deleteFlag: "",
            }
            sub.addRow(row);
        }


        Date.prototype.Format = function (fmt) {
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "H+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }
    </script>
</body>
</html>